﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.Sys
{
    public class ServiceProduct : Benefit.Interface.Sys.IProduct
    {
        Benefit.DB.DBManager db = null;
        public ServiceProduct(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取所有品种
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.Product> GetList()
        {
            var query = from t in db.Product select t;
            List<Models.Sys.Product> products = query.ToList();
            return products;
        }
        /// <summary>
        /// 保存品种
        /// </summary>
        /// <returns></returns>
        public int InsertIntoBenefitFromServer(string productId, string exchangeId)
        {
            Models.Sys.Product p = new Models.Sys.Product();
            p.ProductId = productId;
            var query = from t in db.Exchange where t.ExchangeId.Equals(exchangeId) select t;
            if (query.Count() > 0)
            {
                p.ExchangeID = query.First().Id;
            }
            db.Product.Add(p);
            db.SaveChanges();
            int id = p.Id;
            return id;
        }

        /// <summary>
        /// 获取分析系统里面的品种的编号
        /// </summary>
        /// <param name="Instrument"></param>
        /// <returns></returns>
        public int GetProductId(string productId)
        {
            int id = 0;
            var query = db.Product.Where(a => a.ProductId.Equals(productId));
            if (query.Count() > 0)
            {
                id = query.First().Id;
            }
            return id;
        }
    }
}